package com.czy.util.json;

import com.czy.convert.Converter;
import com.czy.util.time.TimeFormat;
import com.czy.util.time.TimeUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

/**
 * date对象-》日期字符串。
 *
 * @author chenzy
 * @since 2019.12.25
 */

public class SerializerDate2Str extends JsonSerializer<Date> implements Converter<Date, String> {
    @Override
    public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(convert(value));
    }

    @Override
    public String convert(Date source) {
        return TimeUtil.time2Str(TimeUtil.date2Time(source), TimeFormat.DEFAULT);
    }
}
